import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver

class SendTest:
    url=""
    driver=""
    def __init__(self):
        self.url = "http://127.0.0.1:8080/client.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
    def sendMsgTest(self, msg):
        #现在是lisi的主页
        wait = WebDriverWait(self.driver, 20)
        # 一开始进入界面时数据还未加载，等待加载完成后代码继续运行
        wait.until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#session-list > li:nth-child(7)"))
        )
        # ------------通过联系人列表，打开与 zhangsan 的聊天---------------
        # 点击联系人列表
        contact_list = wait.until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR, "body > div.client-container > div > div.left > div.tab > div.tab-friend"))
        )
        contact_list.click()
        # 点击zhangsan
        zhangsan_contact = wait.until(
            expected_conditions.visibility_of_element_located(
                (By.XPATH, "//*[@id=\"friend-list\"]/li/h4"))
        )
        zhangsan_contact.click()

        #在消息编辑框中输入内容
        input = wait.until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "body > div.client-container > div > div.right > textarea"))
        )
        input.send_keys(msg)
        #点击发送
        send_button = wait.until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "body > div.client-container > div > div.right > div.ctrl > button"))
        )
        send_button.click()
